package com.cbtx.module.base;

import android.os.Environment;
import android.text.TextUtils;

import java.io.File;

/**
 * author : joker.peng
 * e-mail : joker@fiture.com
 * date   : 2021/6/10 6:14 PM
 * desc   :
 */
public class TokenUtil {

    protected static final String TOKEN_PATH = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + File.separator + ".token";
    protected static final String TOKEN_KEY = SpUtils.TOKEN_KEY;

    public static String getToken() {

        String token = SpUtils.getString(TOKEN_KEY);
        if (TextUtils.isEmpty(token)) {
            token = FileTools.getContent(TOKEN_PATH);
        }
        return token;
    }

    public static void saveToken(String token) {
        if (TextUtils.isEmpty(token)) {
            return;
        }
        SpUtils.putString(TOKEN_KEY, token);
        if (FileTools.isFileExist(TOKEN_PATH)) {
            FileTools.delete(TOKEN_PATH);
        }
        FileTools.createNewFile(TOKEN_PATH, token);
    }
}
